class SupervisionsController < ApplicationController
  
  before_filter :login_required
  
  # GET /rooms
  # GET /rooms.xml
  def tree
    @title = "Tree Rooms"
    @companies = Company.all
    @computers = []
    for computer in Computer.all
      if (computer.room_id == nil) 
        @computers << computer
      end
    end
    
    respond_to do |format|
      format.html # tree.html.erb
      format.xml  { render :xml => @companies }
    end
  end
  
  def move_computer
    computer = Computer.find(params[:computer])
    
    if (computer.room_id.to_i == params[:room].to_i)
      render :nothing => true
    else
      room = Room.find(params[:room])
      if (room.capacity > room.computers.size)
        oldRoom = Room.find(computer.room_id)
        computer.update_attribute(:room_id, params[:room])
        render :update do |page|
          page.remove "computer_#{params[:computer]}"
          page.insert_html :bottom , "room_#{params[:room] }", :partial => 'supervisions/computer_in_room', :locals => {:computer => computer}
          page.visual_effect(:pulsate, "computer_#{params[:computer]}")
          page.replace_html "room_capacity_#{params[:room]}", :partial => 'supervisions/capacity_room', :locals => {:room => room}
          page.replace_html "room_capacity_#{oldRoom.id}", :partial => 'supervisions/capacity_room', :locals => {:room => oldRoom}
        end
      else
        render :nothing => true
      end
    end
  end
  
  ComputerByStartDate = Struct.new(:status, :count)
  ComputerCountByOs = Struct.new(:os, :count)

  def trash_computer
    computer = Computer.find(params[:computer])
    room = Room.find(params[:room])
    computer.destroy
      
    @computers_counts = Computer.count(:group => "status").collect do |status, count|
      ComputerByStartDate.new(status, count)
    end
    
    @computer_os_counts = Computer.count( :group => "os" ).collect do |os, count|
      ComputerCountByOs.new(os, count)
    end
    render :update do |page|
      page.remove "computer_#{params[:computer]}"
      page.replace_html 'overview_panel', :partial => 'layouts/lateral_panel'
      page.replace_html 'computer_graph', :partial => 'supervisions/computer_report', :locals => {:computer_counts => @computer_counts}
      page.replace_html 'os_graph', :partial => 'supervisions/os_report', :locals => {:computer_os_counts => @computer_os_counts}
      page.visual_effect(:highlight, "trash")
      page.replace_html "room_capacity_#{params[:room]}", :partial => 'supervisions/capacity_room', :locals => {:room => room}
    end
  end
  
  def delete_room_computer
    computer = Computer.find(params[:computer])
    
    computer.update_attribute(:room_id, nil)
      
    render :update do |page|
      page.remove "computer_#{params[:computer]}"
      page.insert_html :bottom , "delete_room", :partial => 'supervisions/computer_in_room', :locals => {:computer => computer}
      page.visual_effect(:pulsate, "computer_#{params[:computer]}")
    end
  end
  
  def add_operation
    @computer = Computer.find(params[:computer])
      
    render :update do |page|
      page.replace_html 'addOperation', :partial => 'computers/edit'
    end
  end
  
  def remote_operation
    @computer = Computer.find(params[:id])
    @computer.update_attributes(params[:computer])
    render :update do |page|
      page.replace_html 'addOperation', ""
      page.remove "computer_#{params[:id]}"
      page.replace_html 'overview_panel', :partial => 'layouts/lateral_panel'
      page.insert_html :bottom , "room_#{@computer.room.id}", :partial => 'supervisions/computer_in_room', :locals => {:computer => @computer}
    end
  end
  
end
